package cn.com.cetccst.datacenter.policy.entity;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
import androidx.room.TypeConverter;
import androidx.room.TypeConverters;

import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

import cn.com.cetccst.datacenter.DataUtils;
import cn.com.cetccst.datacenter.policy.fence.GFence;
import cn.com.cetccst.datacenter.policy.fence.GeoFenceArea;
import cn.com.cetccst.datacenter.policy.fence.TimeFence;

@Entity(tableName = RestrictionEntity.TABLE_NAME)
@TypeConverters({RestrictionEntity.TimeFenceConverter.class, RestrictionEntity.GFenceConverter.class})
public class RestrictionEntity {
    public static final String TABLE_NAME = "restriction_entity";

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    private long id;

    @ColumnInfo(name = "name")
    private String name;

    @ColumnInfo(name = "value")
    private int value;

    @ColumnInfo(name = "time_fence")
    private List<TimeFence> timeFence;

    @ColumnInfo(name = "geo_fence")
    private List<GeoFenceArea> geoFence;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public List<TimeFence> getTimeFence() {
        return timeFence;
    }

    public void setTimeFence(List<TimeFence> timeFence) {
        this.timeFence = timeFence;
    }

    public List<GeoFenceArea> getGeoFence() {
        return geoFence;
    }

    public void setGeoFence(List<GeoFenceArea> geoFence) {
        this.geoFence = geoFence;
    }

    public static class GFenceConverter {
        @TypeConverter
        public List<GeoFenceArea> str2GFence(String value) {
            Type typeToken = new TypeToken<List<GeoFenceArea>>() {
            }.getType();
            return DataUtils.getGson().fromJson(value, typeToken);
        }

        @TypeConverter
        public String GFence2str(List<GeoFenceArea> value) {
            return DataUtils.getGson().toJson(value);
        }
    }

    public static class TimeFenceConverter {
        @TypeConverter
        public List<TimeFence> str2TimeFence(String value) {
            Type typeToken = new TypeToken<List<TimeFence>>() {
            }.getType();
            return DataUtils.getGson().fromJson(value, typeToken);
        }

        @TypeConverter
        public String TimeFence2str(List<TimeFence> value) {
            return DataUtils.getGson().toJson(value);
        }
    }
}
